<?php

// $int = "fds";
// if (!filter_var($int, FILTER_VALIDATE_INT)) {
//     echo("Integer is not valid") . '<br />';
// } else {
//     echo("Integer is valid") . '<br />';
// }

// $var = 300;
// $int_options = array(
//     "options" => array(
//         "min_range" => 0,
//         "max_range" => 256,
//     ),
// );

// if (!filter_var($var, FILTER_VALIDATE_INT, $int_options)) {
//     echo("Integer is not valid").'<br />';
// } else {
//     echo("Integer is valid").'<br />';
// }

$servername = "127.0.0.1:3306";
$username = "root";
$password = "zyenongqaz1";
$dbname = "my_db";

$con = mysqli_connect($servername, $username, $password, $dbname);

if ($con->connect_errno) {
    die('Could not connect: ' . mysql_error());
} else{  
    echo"php env successful".'<br/>';  
}  
// $creatDB = "CREATE DATABASE my_db";
// if ($con->query($creatDB) ) {

//     echo('Database created').'<br />';
// }else{

//     echo "Error creating database: " . mysql_error().'<br />';
// }


// $creatTable = "CREATE TABLE MyGuests(
//     id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
//     firstname VARCHAR(30) NOT NULL,
//     lastname VARCHAR(30) NOT NULL,
//     email VARCHAR(50),
//     reg_date TIMESTAMP
// )";

// if ( $con->query($creatTable) === TRUE ) {
//   echo "Table MyGuests created successfully" .'<br/>';
  
// }else{
//     echo "创建表失败:". mysql_error().'<br/>';

// }

// $creatTable = "CREATE TABLE Persons(
//     id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
//     firstname VARCHAR(30) NOT NULL,
//     lastname VARCHAR(30) NOT NULL,
//     email VARCHAR(50),
//     age INT(6) NOT NULL,
//     reg_date TIMESTAMP
// )";

// if ( $con->query($creatTable) === TRUE ) {
//   echo "Table MyGuests created successfully" .'<br/>';
  
// }else{
//     echo "创建表失败:". mysql_error().'<br/>';

// }


// $insertData = "INSERT INTO MyGuests(firstname, lastname, email) VALUE ('Zhen','Yong','zyenong@126.com')";


// if ( $con->query($insertData) === TRUE ) {
//     echo "新记录插入成功".'<br/>';
// }else{

//     echo "Error: " . $sql . "<br>" . $conn->error;
// }




// 使用mysql预处理语句

// $stmt = $con->prepare ("INSERT INTO Persons(firstname, lastname, email,age) VALUE (?,?,?,?)");
// $stmt->bind_param("sssi",$firstname, $lastname, $email,$age);

// // 设置参数并执行
// $firstname = "John";
// $lastname = "Doe";
// $email = "john@example.com";
// $age=10;
// $stmt->execute();

// $firstname = "Mary";
// $lastname = "Moe";
// $email = "mary@example.com";
// $age=17;
// $stmt->execute();
 
// $firstname = "Julie";
// $lastname = "Dooley";
// $email = "julie@example.com";
// $age=4;
// $stmt->execute();
// $stmt->close();

// $selectSql = "SELECT id, firstname, lastname,age FROM Persons";
// $result = $con->query($selectSql);

// if ($result->num_rows > 0) {
//     while ($row = mysqli_fetch_assoc($result)) {
//         echo "id:". $row["id"] ."-age:".$row["age"]. "--name:".$row["firstname"]."--lastname:".$row["lastname"]."<br/>";
//     }
// }

// $selectWhereSql = "SELECT * FROM MyGuests WHERE firstname='Zhen'";
// $seelctWhereResult = $con->query($selectWhereSql);


// while ($row = mysqli_fetch_array($seelctWhereResult)) {
//     echo "查询到firstname等于Zhen的人的id=".$row["id"]."<br/>";
// }


// $selectOrderSql = "SELECT * FROM Persons ORDER BY age DESC";
// $seelctOrderResult = $con->query($selectOrderSql);


// while ($row = mysqli_fetch_array($seelctOrderResult)) {
//     echo "按年龄排序"."<br/>";
//     echo "id:". $row["id"] ."-age:".$row["age"]. "--name:".$row["firstname"]."--lastname:".$row["lastname"]."<br/>";
// }


// $updateSql = "UPDATE Persons SET firstname='Zhen' WHERE id=1";
// mysqli_query($con,$updateSql);


$delectSql = "DELETE FROM Persons WHERE firstname='Mary'";
mysqli_query($con,$delectSql);


$con->close();